package com.aurora.blog.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.aurora.blog.domain.vo.BlogPostsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aurora.common.log.annotation.Log;
import com.aurora.common.log.enums.BusinessType;
import com.aurora.common.security.annotation.RequiresPermissions;
import com.aurora.blog.domain.BlogPosts;
import com.aurora.blog.service.IBlogPostsService;
import com.aurora.common.core.web.controller.BaseController;
import com.aurora.common.core.web.domain.AjaxResult;
import com.aurora.common.core.utils.poi.ExcelUtil;
import com.aurora.common.core.web.page.TableDataInfo;

/**
 * 文章Controller
 * 
 * @author aurora
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/blogPosts")
public class BlogPostsController extends BaseController
{
    @Autowired
    private IBlogPostsService blogPostsService;

    /**
     * 查询文章列表
     */
    @RequiresPermissions("blog:blogPosts:list")
    @GetMapping("/list")
    public TableDataInfo list(BlogPostsVo blogPosts)
    {
        startPage();
        List<BlogPostsVo> list = blogPostsService.selectBlogPostsList(blogPosts);
        return getDataTable(list);
    }

    /**
     * 查询文章列表
     */
    @GetMapping("/getBlogPostsList")
    public AjaxResult getBlogPostsList(BlogPostsVo blogPosts)
    {
        List<BlogPostsVo> list = blogPostsService.selectBlogPostsList(blogPosts);
        return success(list);
    }

    /**
     * 导出文章列表
     */
    @RequiresPermissions("blog:blogPosts:export")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogPostsVo blogPosts)
    {
        List<BlogPostsVo> list = blogPostsService.selectBlogPostsList(blogPosts);
        ExcelUtil<BlogPostsVo> util = new ExcelUtil<BlogPostsVo>(BlogPostsVo.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @RequiresPermissions("blog:blogPosts:query")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return success(blogPostsService.selectBlogPostsByPostId(postId));
    }

    /**
     * 新增文章
     */
    @RequiresPermissions("blog:blogPosts:add")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogPostsVo blogPostsVo)
    {
        return toAjax(blogPostsService.insertBlogPosts(blogPostsVo));
    }

    /**
     * 修改文章
     */
    @RequiresPermissions("blog:blogPosts:edit")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogPostsVo blogPostsVo)
    {
        return toAjax(blogPostsService.updateBlogPosts(blogPostsVo));
    }

    /**
     * 删除文章
     */
    @RequiresPermissions("blog:blogPosts:remove")
    @Log(title = "文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(blogPostsService.deleteBlogPostsByPostIds(postIds));
    }
}
